<script setup>
import { Edit, Delete, Search, Warning, Unlock, Lock } from "@element-plus/icons-vue";
import { ElMessage, ElMessageBox } from 'element-plus'

import { ref } from "vue";
const title = ref("");
const userRole = ref();
const username = ref();
const dialogUserVisible = ref();
const visibleAddEditor = ref(false);
//分页条数据模型
const pageNum = ref(1); //当前页
const total = ref(20); //总条数
const pageSize = ref(5); //每页条数

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
  pageSize.value = size;
  userSearch();
};
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  pageNum.value = num;
  userSearch();
};
const dialogVisible = ref(false);

import { editorSearchService,editorDeleteService,editorUpdateStateService} from '@/api/user.js';

import { articleCategoryAllListService } from '@/api/article.js'

 //获取分类列表
const categorys= ref();
const articleCategoryList = () =>{
    let result = articleCategoryAllListService();
    categorys.value = result.data;
}

const user = ref([]);

/**
 * 查询所有的用户
 */
const userSearch = async() => {
    let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
    username: username.value,
    // userRole: userRole.value,
  };
  let result = await editorSearchService(params);
  total.value = result.data.total;
  user.value = result.data.items;
}
userSearch();

const deleteUser = (row) => {
  ElMessageBox.confirm("确认删除该用户吗？", "温馨提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      let result = await editorDeleteService(row.id);
      ElMessage({
        type: "success",
        message: "删除成功",
      });
      userSearch();
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "删除失败",
      });
    });
  dialogVisible.value = false;
  userSearch();
};


const updateEditorState = (row) => {
  ElMessageBox.confirm("确认修改用户的账号状态吗？", "温馨提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      // updateState(row)
      await editorUpdateStateService(row.id,row.state);
      ElMessage({
        type: "success",
        message: "更改成功",
      });
      userSearch();
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消更改",
      });
    });
  dialogVisible.value = false;
  userSearch();
};

</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>编辑管理</span>
      </div>

    </template>
    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item label="编辑账号">
        <el-input placeholder="请输入编辑账号" v-model="username">
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="userSearch()">搜索</el-button>
        <el-button
          @click="
            username = '';
            userSearch();
          "
          >重置</el-button
        >
      </el-form-item>

    </el-form>
    <!-- 文章列表 -->
    <el-table :data="user" style="width: 100%">
      <el-table-column label="编辑账号" width="200" prop="username">
      </el-table-column>
      <el-table-column label="编辑头像" width="100" prop="userPic">
        <template #default="scope">
             <div style="display: flex; align-items: center">
             <el-image :src="scope.row.userPic"/>
             </div>
        </template>
      </el-table-column>
      <el-table-column label="编辑分类" prop = "categoryName" align="center">
      </el-table-column>
      <el-table-column label="编辑昵称" prop="nickname" align="center"></el-table-column>
      <el-table-column label="编辑邮箱" prop="email" align="center"></el-table-column>
      <el-table-column label="编辑角色" prop="userRole" align="center">
        <template #default="scope">
          <span v-if="scope.row.userRole == 1"> 编辑</span>
          <span v-if="scope.row.userRole == 0"> 作家</span>
             
        </template>
      </el-table-column>
            <el-table-column label="编辑状态" prop="state" align="center">
        <template #default="scope">
          <span v-if="scope.row.state == 1"><font color="red"> 异常</font></span>
          <span v-if="scope.row.state == 0"> <font color="green">正常 </font></span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="145" align="center">
        <template #default="{ row }">
          
         <el-button v-if="row.state == 0"
            :icon="Unlock"
            circle
            plain
            type="primary"
            @click="updateEditorState(row)"
          ></el-button>
          <el-button v-if="row.state == 1"
            :icon="Lock"
            circle
            plain
            type="warning"
            @click="updateEditorState(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="deleteUser(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    
    <!-- 分页条 -->
    <el-pagination
      v-model:current-page="pageNum"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 15, 20]"
      layout="jumper, total, sizes, prev, pager, next"
      background
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
  </el-card>
   <el-dialog
    v-model="dialogArticleVisible"
    title="文章详情"
    width="80%"
    :before-close="handleClose"
  >
    <div v-html="content1"></div>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="dialogArticleVisible = false">
          确认
        </el-button>
      </span>
    </template>
  </el-dialog>

  <!--用户信息修改-->
    <el-dialog
    v-model="dialogUserVisible"
    title="用户信息修改："
    width="40%"
    :before-close="handleClose"
  >
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="dialogUserVisible = false">
          确认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>